package com.student.xujiang.myallstudent.Rxlifecycle;


import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.trello.rxlifecycle2.RxLifecycle;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;

/**
 * Created by xujiang on 2017/3/24.
 */

//http://www.cnblogs.com/liulipeng/p/5050827.html
//http://www.cnblogs.com/zhaoyanjun/p/5523454.html
public class BaseRxLifecycleActivity extends RxAppCompatActivity {
    private final String TAG = BaseRxLifecycleActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.i("TAG","执行测试2");
        Flowable<String> flowable = Flowable.create(new FlowableOnSubscribe<String>() {
            @Override
            public void subscribe(FlowableEmitter<String> e) throws Exception {

                e.onNext("hello RxJava 2");
                e.onComplete();
            }
        }, BackpressureStrategy.BUFFER);

        Subscriber subscriber = new Subscriber<String>() {
            @Override
            public void onSubscribe(Subscription s) {
                /**
                 * 需要注意的是，在onSubscribe中，我们需要调用request去请求资源，参数就是要请求的数量，
                 * 一般如果不限制请求数量，可以写成Long.MAX_VALUE。
                 * 如果你不调用request，Subscriber的onNext和onComplete方法将不会被调用。
                 * */
                Log.i(TAG, "onSubscribe");
                s.request(Long.MAX_VALUE);
            }

            @Override
            public void onNext(String s) {

                Log.i(TAG, "onNext+"+s);
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete");
            }
        };

        //为了让”发射器”和”接收器”工作起来，我们还需要把他们组装在一起。
        flowable
                .compose(this.<String>bindToLifecycle())  //http://www.jianshu.com/p/a3ad9dd20655
                .subscribe(subscriber);
    }


}



